<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_TourChatController extends Zend_Rest_Controller
{

	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {

	}

	public function getAction() {

	}

	public function headAction() {

	}

	public function indexAction() {
		try{
			$tourchat_mapper = new Api_Model_TourChatMapper();
			$tour_id = $this->_getParam('param1');
			$tour_chat_limit = $this->_getParam('batch');
			if ($tour_id != NULL){
				$chat = $tourchat_mapper->getAllChat($tour_id, $tour_chat_limit);
				$this->getresponse()->appendBody(json_encode($chat));
				$this->getResponse()->setHttpResponseCode(200);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function postAction() {
		try{
			$tourchat_mapper = new Api_Model_TourChatMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);
			if ($data['tour_id'] != "" || $data['user_id'] != "" || $data['content'] != ""){

				$tourchat_mapper->sendTourChat($data);
				throw new Exception(json_encode(array('meta' => 'Created')),201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {

	}
}

